package com.atguigu.lease.web.admin.custom.config;

import com.atguigu.lease.web.admin.custom.converter.StringToBaseEnumConverterFactory;
import com.atguigu.lease.web.admin.custom.converter.StringToItemTypeConverter;
import com.atguigu.lease.web.admin.custom.interceptor.AuthenticationInterceptor;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author cxt
 * &#064;Description 自定义Web组件(半自动 没加@EnableWebMvc)
 * &#064;create 2025/3/15 下午4:34
 */
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
    @Resource
    private StringToItemTypeConverter stringToItemTypeConverter;
    @Resource
    private StringToBaseEnumConverterFactory stringToBaseEnumConverterFactory;
    @Autowired
    private AuthenticationInterceptor authenticationInterceptor;

    @Override
    public void addFormatters(FormatterRegistry registry) {
        /**
         * 添加Converter【WebDataBinder`依赖于Converter实现类型转换】
         */
//        registry.addConverter(this.stringToItemTypeConverter);
        /**
         * 添加ConverterFactory
         */
        registry.addConverterFactory(this.stringToBaseEnumConverterFactory);
    }
//  添加自定义拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(this.authenticationInterceptor)
                .addPathPatterns("/admin/**") //设置拦截范围
                .excludePathPatterns("/admin/login/**"); //设置排除拦截范围
    }
}
